require "proto/partner/message/ReqUpLevelPartnerMessage"
require "proto/partner/message/ReqUpStarPartnerMessage"
require "proto/partner/message/ReqAssemblePartnerMessage"
require "proto/partner/message/ReqHandbookRewardMessage"
require "proto/partner/message/ReqPartnerResetMessage"
require "proto/partner/message/ReqPartnerActivateBondMessage"
require "proto/partner/message/ReqBondPointRewardMessage"
require "proto/partner/message/ReqBondRewardListMessage"
require "proto/partner/message/ReqPartnerPowerMessage"
require "proto/partner/message/ReqUnLockPartnerMessage"

---英雄评论相关协议
require "proto/herocomment/message/ReqHeroCommentMessage"
require "proto/herocomment/message/ReqGetHeroCommentsMessage"
require "proto/herocomment/message/ReqHeroCommentLikeMessage"
require "proto/herocomment/message/ReqHeroCommentPraiseMessage"
---end
require "proto/partner/message/ReqPartnerDetailMessage"
require "proto/partner/message/ReqPartnerPopularityRankMessage"
require "proto/partner/message/ReqShareHeroRewardMessage"
require "proto/partner/message/ReqPartnerInteractMessage"
require "proto/partner/message/ReqPartnerFavorRewardGetMessage"
require "proto/partner/message/ReqPartnerInteractGiveMessage"
---阵容推荐相关协议
require "proto/formationRecommand/message/ReqFormationRecClickMessage"
require "proto/formationRecommand/message/ReqFormationRecInfoMessage"
require "proto/formationRecommand/message/ReqFormationRecReceiveMessage"
---end

---英雄置换相关协议
require "proto/partner/message/ReqRechangePartnerMessage"
---end
---智慧之链继承
require "proto/partner/message/ReqExtendLevelMessage"

partnerHandler = partnerHandler or {};
G.serverData = G.serverData or {};
partnerHandler.canClickFetterAward=true
partnerHandler.interactionSaveData=
{
    isEffective=false,
    partnerDatas={},    --partnerId=0,lastLevel=0,lastExp=0
    interactionId=0,
    isBattle=false,
}
-- 伙伴信息
function partnerHandler.receivePartnerInfo(msg)
    --log("partnerHandler.receivePartnerInfo");
    --dataTable.PrintTable(msg);
    if msg.state == 0 then
        local haveData = false
        for i = 1, #msg.partners do
            haveData = true
            local partner = msg.partners[i];
            logicMgr.HeroManager.setHeroPartnerId(partner.partnerModelId, partner.partnerId);
            if partner.level ~= nil then
                logicMgr.HeroManager.setHeroLevel(partner.partnerId, partner.level);
            end
            if partner.star ~= nil then
                logicMgr.HeroManager.setHeroStar(partner.partnerId, partner.star);
            end
            if partner.exp ~= nil then
                logicMgr.HeroManager.setHeroExp(partner.partnerId, partner.exp);
            end
            if partner.activateBondIds ~= nil then
                logicMgr.HeroManager.setHeroFetter(partner.partnerId, partner.activateBondIds);
            end
            if partner.sameNum ~= nil then
                logicMgr.HeroManager.setHeroSameNum(partner.partnerId, partner.sameNum);
            end
            if partner.fightVal ~= nil then
                logicMgr.HeroManager.setHeroPower(partner.partnerId, partner.fightVal)
            end
            if partner.fightVal2 ~= nil then
                logicMgr.HeroManager.setHeroRealPower(partner.partnerId, partner.fightVal2)
            end
            logicMgr.HeroManager.setHeroInteraction(partner)
            logicMgr.HeroManager.updateHeroBaseAttr(partner.partnerId, nil, nil, nil, true);
        end
        if haveData then
            --logicMgr.HeroLinkManager.updateHeroForLinkMain();
            if msg.activateBondIds ~= nil then
                logicMgr.HeroManager.SetForverHeroFetter(msg.activateBondIds)
            end
            logicMgr.HeroManager.updateInteractionNum()
            logicMgr.HeroManager.CalculateAllFitterAttr()
            logicMgr.HeroManager.RefreshHeadFrameAtt()
            HeroManager.RefreshHeroAdd()
        end
    end
end

function partnerHandler.receiveHandbookInfo(msg)
    if msg.state == 0 then
        logicMgr.HeroManager.initHandbook(msg.handbooks);
        FireEvent(Config.EventType.Fresh_Handbook);
    end
end
---partnerId 0 全部
function partnerHandler.ReqHeroPower(partnerId,noShowFont)
    --local req = ReqPartnerPowerMessage.New()
    --req.partnerId = partnerId
    --sendReqMessage(req)  
    if logicMgr.HeroManager == nil then
        return
    end

    if partnerId == 0 then
        local heroList = logicMgr.HeroManager.getOwnerHeroListNoClone(0);
        for _, hero in pairs(heroList) do
            logicMgr.HeroManager.setHeroPower(hero["partnerId"], logicMgr.HeroManager.CalcHeroPower(hero["partnerId"]))
            logicMgr.HeroManager.setHeroRealPower(hero["partnerId"], logicMgr.HeroManager.CalcHeroPower(hero["partnerId"],true,true))
            HeroManager.SetAllPower(hero["partnerId"],nil,true)

            if not noShowFont then
                local num1,num2 = logicMgr.HeroManager.SetAllPower()
                if num1 ~= num2 then
                    logicMgr.HeroManager.ShowFont()
                end
            end

        end
        if not isOpenUI(PanelNames.UICharactorInfoHero) then
            FireEvent(Config.EventType.ReFreshHeroPower)
        end
    else
        logicMgr.HeroManager.setHeroPower(partnerId, logicMgr.HeroManager.CalcHeroPower(partnerId))
        logicMgr.HeroManager.setHeroRealPower(partnerId, logicMgr.HeroManager.CalcHeroPower(partnerId,true,true))
        HeroManager.SetAllPower(partnerId,nil,true)

        if not noShowFont  then
            local num1,num2 = logicMgr.HeroManager.SetAllPower()
            if num1 ~= num2 then
                logicMgr.HeroManager.ShowFont()
            end
        end
        if not isOpenUI(PanelNames.UICharactorInfoHero) then
            FireEvent(Config.EventType.ReFreshHeroPower)
        end
    end

    logicMgr.TATrackCollect.Track_AllPower()
    logicMgr.TATrackCollect.Track_topHerosTwelve()
end

function partnerHandler.ResHeroPower(msg)
    if msg.state == 0 then
        for _, v in pairs(msg.PartnerPowerInfoList) do
            logicMgr.HeroManager.setHeroPower(v.partnerId, v.power)
            logicMgr.HeroManager.setHeroRealPower(v.partnerId, v.fightVal2)

            local calcPower = logicMgr.HeroManager.CalcHeroPower(v.partnerId);
            logWarn("CaclPower:"..(calcPower));
            if #msg.PartnerPowerInfoList > 0 and tostring(v.partnerId)~="0" then
                logicMgr.HeroManager.SetAllPower(v.partnerId,nil,true)
                local num1,num2 = logicMgr.HeroManager.SetAllPower()
                if num1 ~= num2 then
                    logicMgr.HeroManager.ShowFont()
                end
            end
        end
        if not isOpenUI(PanelNames.UICharactorInfoHero) then
            FireEvent(Config.EventType.ReFreshHeroPower)
        end
    end

    logicMgr.TATrackCollect.Track_AllPower()
    logicMgr.TATrackCollect.Track_topHerosTwelve()
end

function partnerHandler.receiveUpLevelPartner(msg)
    if msg.state == 0 then
        for i = 1, #msg.partner do
            local partner = msg.partner[i]
            local _lastLv = logicMgr.HeroManager.GetHeroLevel(partner.partnerId)

            logicMgr.HeroManager.setHeroSameNum(partner.partnerId, partner.sameNum)
            logicMgr.HeroManager.setHeroLevel(partner.partnerId, partner.level);
            logicMgr.HeroManager.updateHeroBaseAttr(partner.partnerId, true, true);

            --英雄升级TA
            local _levelUpDic = {}
            _levelUpDic.hero_uniqueid = tostring(partner.partnerId)
            _levelUpDic.hero_id = tostring(partner.partnerModelId)
            _levelUpDic.hero_level = tonumber(partner.level)
            _levelUpDic.level_up_number = tonumber(partner.level) - _lastLv
            TAManager.TrackByDictionary("hero_level_up", _levelUpDic)
        end
        FireEvent(Config.EventType.Fresh_Effect_Level)
        
        FireEvent(Config.EventType.Fresh_Skill);
        logicMgr.HeroLinkManager.updateHeroForLinkMain()
        --local heroList = logicMgr.HeroManager.getOwnerHeroListReadOnly()
        --for _,v in pairs(heroList) do
        --    logicMgr.HeroManager.updateHeroBaseAttr(tostring(v.partnerId), false);
        --end

        FireEvent(Config.EventType.Fresh_Level, 1)
    end
end

function partnerHandler.receiveUpStarPartner(msg)
    if msg.state == 0 then
        print("收到消息======",Time.realtimeSinceStartup)
        --msg.partner 里只会有一个英雄
        for i = 1, #msg.partner do
            local partner = msg.partner[i];
            local _heroInfo = logicMgr.HeroManager.getHeroDatabyPartnerId(tostring(partner.partnerId),true)
            local _currentSameNum = ItemManager.getItemCountbyModelId(_heroInfo.storeId);
            local _lastStar = logicMgr.HeroManager.GetHeroStar(partner.partnerId)

            logicMgr.HeroManager.setHeroStar(partner.partnerId, partner.star);
            logicMgr.HeroManager.setHeroSameNum(partner.partnerId, partner.sameNum)
            logicMgr.HeroManager.updateHeroBaseAttr(partner.partnerId,true,nil,nil,nil,true);
            --if tostring(partner.partnerId) == logicMgr.HeroManager.getMyself().partnerId then
            --    logicMgr.HeroManager.resetImitationSkill();
            --end
            -- 英雄突破埋点
            local _upStarDic = {}
            _upStarDic.hero_uniqueid = tostring(partner.partnerId)
            _upStarDic.hero_id = tostring(partner.partnerModelId)
            _upStarDic.current_hero_star = tonumber(partner.star)
            _upStarDic.last_hero_star = tonumber(_lastStar)
            _upStarDic.current_soulstone = tonumber(_currentSameNum)
            _upStarDic.last_soulstone = tonumber(partnerHandler.LastSameNum)
            TAManager.TrackByDictionary("hero_rank_up", _upStarDic)
        end
        HeroManager.RefreshHeroAdd();
        --logicMgr.HeroLinkManager.updateHeroForLinkMain()
        print("逻辑处理完=====",Time.realtimeSinceStartup)
        local star=msg.partner[1].star
        if star>80 and (star-80)%10~=0 then
            partnerHandler.ShowBreakPointEffect=true
            FireEvent(Config.EventType.Fresh_Star,true);
        else
            FireEvent(Config.EventType.Fresh_Star);
        end
    end
end

function partnerHandler.receiveAddPartner(msg)
    if msg.state == 0 then
        for i = 1, #msg.partners do
            local partner = msg.partners[i];
            logicMgr.HeroManager.setHeroPartnerId(partner.partnerModelId, partner.partnerId, true);
            logicMgr.HeroManager.setHeroLevel(partner.partnerId, partner.level);
            logicMgr.HeroManager.setHeroStar(partner.partnerId, partner.star);
            logicMgr.HeroManager.setHeroExp(partner.partnerId, partner.exp);
            logicMgr.HeroManager.updateHeroBaseAttr(partner.partnerId);
            logicMgr.HeroManager.setHandbook(tostring(partner.partnerModelId), false);
            logicMgr.HeroManager.setHeroSameNum(partner.partnerId, partner.sameNum)
            Util.SetPlayerPrefs(logicMgr.HeroManager.GetRoleInfo().playerId .. Config.EventType.Fresh_HeroSkill .. partner.partnerModelId, 1);
            logicMgr.HeroLinkManager.updateHeroForLinkMain(2, {newPartner = tostring(partner.partnerId)});
        end
        for k, v in pairs(msg.weaponInfo) do
            logicMgr.EquipManager.AddWeapon(v)
        end
        FireEvent(Config.EventType.Fresh_HeroSkill);
        FireEvent(Config.EventType.Fresh_Mercenary);
        FireEvent(Config.EventType.Fresh_Handbook);
        HeroManager.RefreshHeroAdd();

        -- iOS Widget: 更新英雄数量
        if CompareOriginVersion("1.4.2.0001") then
            local heroList = logicMgr.HeroManager.getOwnerHeroListReadOnly()
            LoginSDK.GetInstance():SetWidgetUserHeros(#heroList)
        end
    end
end

function partnerHandler.receiveAssemblePartner(msg)
    if msg.state == 0 then

    end
end

function partnerHandler.receivePartnerReset(msg)
    if msg.state == 0 then
        logicMgr.HeroManager.resetHero(tostring(msg.partnerId));
        logicMgr.EquipManager.delEquip(msg.partnerId, EnumConst.EquipPartEnum.BODY);
        logicMgr.EquipManager.delEquip(msg.partnerId, EnumConst.EquipPartEnum.HAND);
        logicMgr.EquipManager.delEquip(msg.partnerId, EnumConst.EquipPartEnum.HEAD);
        logicMgr.EquipManager.delEquip(msg.partnerId, EnumConst.EquipPartEnum.LEG);
        logicMgr.RuneManager.TaskOffHeroRune(tostring(msg.partnerId))
        --[[local weapon = logicMgr.EquipManager.tabWeapon[tostring(msg.partnerId)];
        if weapon ~= nil then
            weapon.attiredId = 0;
            weapon.level = 0;
        end--]]
    end
    logicMgr.HeroLinkManager.updateHeroForLinkMain();
    FireEvent(Config.EventType.Fresh_HeroReborn);
    FireEvent(Config.EventType.Fresh_HeroStar);
    FireEvent(Config.EventType.Fresh_Level,2);
    FireEvent(Config.EventType.Fresh_Skill);
end

function partnerHandler.receiveHandbookReward(msg)
    if msg.state == 0 then
        logicMgr.HeroManager.setHandbook(tostring(msg.partnerModelId), true);
        local cfgInfo = HelperFunc.GetGlobalVlue(56)
        local heroCfgInfo = G.dataTable["t_hero"][tostring(msg.partnerModelId)]
        local qualityValue = heroCfgInfo["f_HeroInitialStar"]
        for _, v in pairs(cfgInfo) do
            --英雄品质,物品id,物品数量
            local quality = tonumber(v[1])
            local itemTabID = tonumber(v[2])
            local num = tonumber(v[3])
            if qualityValue == quality then
                FireEvent(Config.EventType.Fresh_HandbookRewardSuc,itemTabID,num)
                --showUI(PanelNames.UICommonItemGet, itemTabID, num, false)
                break
            end
        end
        FireEvent(Config.EventType.Fresh_Handbook);
    end
end

--英雄升级
function partnerHandler.sendUpLevelMessage(partnerId, num)
    AudioManager.IsHeroLevelUp = true
    local req = ReqUpLevelPartnerMessage.New()
    req.partnerId = partnerId
    req.num = num or 1
    sendReqMessage(req)
end

--英雄升星
function partnerHandler.sendUpStarMessage(partnerId, currentSameNum)
    partnerHandler.LastSameNum = currentSameNum or 0
    local req = ReqUpStarPartnerMessage.New();
    req.partnerId = partnerId;
    print("发消息======",Time.realtimeSinceStartup)
    sendReqMessage(req);
end

--英雄合成
function partnerHandler.sendAssembleMessage(partnerModelId)
    local req = ReqAssemblePartnerMessage.New();
    req.partnerModelId = partnerModelId;
    sendReqMessage(req);
end

--英雄重生
function partnerHandler.sendPartnerReset(partnerId)
    local req = ReqPartnerResetMessage.New();
    req.partnerId = partnerId;
    sendReqMessage(req);
end

--英雄图鉴领取
function partnerHandler.sendHandbookReward(partnerModelId)
    local req = ReqHandbookRewardMessage.New();
    req.partnerModelId = partnerModelId;
    sendReqMessage(req);
end

function partnerHandler.ReturnFightState(heroid)
    if heroid == MercenaryModel.partnerFormates then
        return true;
    else
        return indexofTable(MercenaryModel.assitParterner, heroid) >= 1;
    end
end

--- 羁绊激活
function partnerHandler.receiveActiveFetter(msg)
    if msg.state == 0 then
        local oldAtt = HeroManager.GetCalculateAllAttr()
        for j, l in pairs(msg.activateBondInfoList) do
            for k, v in pairs(l.bondId) do
                logicMgr.HeroManager.appendHeroFetter(l.partnerId, v)
                logicMgr.HeroManager.ChangeForverHeroFetter(v)
                local _rewardDic = {}
                _rewardDic.trammelpoint_id = tostring(v)
                TAManager.TrackByDictionary("trammel_point_reward", _rewardDic)
            end
            logicMgr.HeroManager.updateHeroBaseAttr(l.partnerId,false, nil, true)
        end

        logicMgr.HeroManager.CalculateAllFitterAttr()
        FireEvent(Config.EventType.Fresh_FetterDetial)
    
        partnerHandler.ReqHeroPower(0)
        
        FireEvent(Config.EventType.FetterParticle)

        local newAtt = HeroManager.GetCalculateAllAttr();
        HeroManager.UpTipsAtt( newAtt[1].attrNum ,oldAtt[1].attrNum,
                 newAtt[2].attrNum ,oldAtt[2].attrNum,
                 newAtt[3].attrNum ,oldAtt[3].attrNum,true)
        
        --utimer.delayCall(function()
        --    HeroManager.ClerPool()
        --end, 1000 )
    end

    logicMgr.TATrackCollect.Track_Fetter()
end

function partnerHandler.sendActiveFetter(param)
    local req = ReqPartnerActivateBondMessage.New();
    req.activateBondInfoList = param;
    sendReqMessage(req);
end

--- 羁绊领奖
function partnerHandler.receiveFetterAward(msg)
    partnerHandler.canClickFetterAward=true
    if msg.state == 0 then
        logicMgr.HeroManager.AddHeroFetterHadAwardList(msg.modelId);
        FireEvent(Config.EventType.Fresh_FetterAward)

        --local data = decodeJsonStrToTabel(G.dataTable["t_trammelpointreward"][tostring(msg.modelId)]["f_Reward"])
        --local _itemModelId = data[1][1]
        --local _itemCount = data[1][2]
        --showUI(PanelNames.UICommonItemGet, _itemModelId, _itemCount);
        if msg.itemInfoList ~= nil and #msg.itemInfoList > 0 then
            showUI(PanelNames.UICommonItemGet, nil, nil, 3, msg.itemInfoList);
        end
        --UISysTips.AddMsg(getLanguage("Sys_TrammelRewardReceived"))

        local _rewardDic = {}
        _rewardDic.trammelpoint_id = tostring(msg.modelId)
        TAManager.TrackByDictionary("trammel_point_reward", _rewardDic)

    end
end

function partnerHandler.sendFetterAward(cfgID)
    local req = ReqBondPointRewardMessage.New()
    req.modelId = cfgID
    sendReqMessage(req)
end

--- 请求获取羁绊奖励 已领取的id列表
function partnerHandler.receiveFetterAwaraList(msg)
    if msg.state == 0 then
        logicMgr.HeroManager.SetHeroFetterHadAwardList(msg.activateBondIds)
        FireEvent(Config.EventType.Fresh_FetterAward)
    end
end

function partnerHandler.sendFetterAwaraList()
    local req = ReqBondRewardListMessage.New()
    sendReqMessage(req)
end



--------------------------------------------------------------------------------
-- 英雄评论相关
--------------------------------------------------------------------------------
-- 发送英雄评论
function partnerHandler.sendHeroCommentMessage( heroid, content) 
    local req = ReqHeroCommentMessage.New();
    req.heroID = heroid
    req.comment = content
    sendReqMessage(req)
end
-- 发送英雄评论返回
function partnerHandler.receiveHeroCommentMessage(msg) 
    local ui_charactor_info_archives = CtrlManager.GetCtrl(PanelNames.UICharactorInfoArchives)
    if ui_charactor_info_archives ~= nil then
        ui_charactor_info_archives.ResSendCommentMessage(msg)
    end
end

-- 拉取英雄评论请求
function partnerHandler.sendGetHeroCommentsMessage( hero_id )
    local req = ReqGetHeroCommentsMessage.New()
    req.heroModelId = hero_id
    sendReqMessage(req)
end
-- 拉取英雄评论返回
function partnerHandler.receiveGetHeroCommentsMessage(msg)
    if isOpenUI(PanelNames.UICharactorInfoArchives) then
        local ui_charactor_info_archives = CtrlManager.GetCtrl(PanelNames.UICharactorInfoArchives)
        if ui_charactor_info_archives ~= nil then
            ui_charactor_info_archives.ResGetHeroCommentsMessage(msg)
        end
    end

    if isOpenUI(PanelNames.UICharactorInfoHero) then
        local ui_charactor_info_hero = CtrlManager.GetCtrl(PanelNames.UICharactorInfoHero)
        if ui_charactor_info_hero ~= nil then
            ui_charactor_info_hero.UpdateCommentNum(msg)
        end
    end

    if isOpenUI(PanelNames.UICharactorPreview) then
        local ui_charactor_preview = CtrlManager.GetCtrl(PanelNames.UICharactorPreview)
        if ui_charactor_preview ~= nil then
            ui_charactor_preview.UpdateCommentNum(msg)
        end
    end
end

-- http下发英雄评论数据
function partnerHandler.HttpGetHeroCommentData( hero_id )
    if partnerHandler.hero_comments_data == nil then
        partnerHandler.hero_comments_data = {}
    end
    hero_id = tostring(hero_id) 
    if partnerHandler.hero_comments_data[hero_id] == nil then
        local url = string.format("%s/%s/%s/HeroCheckList.txt", Config.ServerCommentUrl, Config.GroupId, hero_id)
        -- print("HttpGetHeroCommentDataurl:", url)
        Http.Get(url, nil, function(success, cmdStr)
            -- print("------------->", success, cmdStr)
            if success then 
                if cmdStr and cmdStr ~= "" then
                    local data_json = decodeJsonStrToTabel(cmdStr) or {};
                    local data_item = {} 
                    for _, v in pairs(data_json) do
                        if v then
                            local cid = tostring(v.cid) 
                            data_item[cid] = v
                        end
                    end
                    partnerHandler.hero_comments_data[hero_id] = data_item 
                end 
            end 
            partnerHandler.sendGetHeroCommentsMessage( hero_id ) 
        end) 
    else
        partnerHandler.sendGetHeroCommentsMessage( hero_id ) 
    end

end

function partnerHandler.GetHeroCommentDataById( hero_id, comment_id )
    hero_id = tostring(hero_id) 
    comment_id = tostring(comment_id) 
    if partnerHandler.hero_comments_data and partnerHandler.hero_comments_data[hero_id] then
        return partnerHandler.hero_comments_data[hero_id][comment_id]
    end
end

function partnerHandler.GetHeroCommentDataByHeroId( hero_id )
    hero_id = tostring(hero_id) 
    if partnerHandler.hero_comments_data and partnerHandler.hero_comments_data[hero_id] then
        return partnerHandler.hero_comments_data[hero_id]
    end
    return {}
end

-- 点击喜好当前英雄请求
function partnerHandler.sendLikeHeroMessage( heroid ) 
    local req = ReqHeroCommentLikeMessage.New()
    req.heroID = heroid
    sendReqMessage(req)
end
function partnerHandler.receiveLikeHeroMessage(msg)
    if msg.state == 0 then
        FireEvent(Config.EventType.UpdateLikeHero);
    end
    local ui_charactor_info_archives = CtrlManager.GetCtrl(PanelNames.UICharactorInfoArchives)
    if ui_charactor_info_archives ~= nil then
        ui_charactor_info_archives.ResLikeHeroMessage(msg)
    end
end
-- 点赞评论请求
function partnerHandler.sendGiveLikeCommentMessage( hero_id, comment_id ) 
    local req = ReqHeroCommentPraiseMessage.New()
    req.heroID = tonumber(hero_id)
    req.commentId = tonumber(comment_id)
    sendReqMessage(req)
end
function partnerHandler.receiveGiveLikeCommentMessage(msg) 
    local ui_charactor_info_archives = CtrlManager.GetCtrl(PanelNames.UICharactorInfoArchives)
    if ui_charactor_info_archives ~= nil then
        ui_charactor_info_archives.ResGiveLikeCommentMessage(msg)
    end
end
--请求镜魂解锁英雄
function partnerHandler.sendUnlockPartner(heroId)
    local req = ReqUnLockPartnerMessage.New()
    req.heroConfigID=heroId
    sendReqMessage(req)
end
--接受镜魂解锁英雄
function partnerHandler.receiveUnlockPartner(msg)
    if msg.state==0 then
        if msg.result==0 then
            FireEvent(Config.EventType.ClipUnlockPartner,msg.heroConfigID)
        end
    end
end
--玩家（非自己）英雄信息
function partnerHandler.ReqOtherHeroInfo(playerId,heroid)
    local req = ReqPartnerDetailMessage.New()
    req.playerId = playerId
    req.partnerId = heroid
    sendReqMessage(req)
end
function partnerHandler.ResOtherHeroInfo(msg)
    FireEvent(Config.EventType.UpdateOtherHeroInfo,msg)
end
------好感度------
--领取好感度奖励
function partnerHandler.ReqPartnerFavorRewardGet(partnerId,favorRewardId)
    local req=ReqPartnerFavorRewardGetMessage.New()
    req.partnerId=tostring(partnerId)
    req.favorRewardId=favorRewardId
    sendReqMessage(req)
end

function partnerHandler.ResPartnerFavorRewardGet(msg)
    if msg.state==0 then
        logicMgr.HeroManager.setHeroInteraction(msg.partner)
        if not table.isNullOrEmpty(msg.rewards)then
            showUI(PanelNames.UICommonItemGet,nil,nil,3, msg.rewards)
            FireEvent(Config.EventType.Fresh_HeroInteraction)
        end
    end
end

--进行好感度互动
function partnerHandler.ReqPartnerInteract(partnerIds,interactionId)
    if table.isNullOrEmpty(partnerIds)then
        partnerHandler.clearInteractionSaveData()
        return
    end
    local req=ReqPartnerInteractMessage.New()
    req.partnerIds=partnerIds
    req.interactId=interactionId
    sendReqMessage(req)
end
function partnerHandler.ResPartnerInteract(msg)
    if msg.state==0 then
        for i,v in ipairs(msg.partners)do
            logicMgr.HeroManager.setHeroInteraction(v)
        end
        logicMgr.HeroManager.updateInteractionNum(true)
        FireEvent(Config.EventType.Fresh_HeroInteraction)
        if partnerHandler.isInteractionEffective() then
            local flag=false
            local saveData =partnerHandler.interactionSaveData
            local favorUpDatas={}
            for i,v in ipairs(saveData.partnerDatas)do
                local heroData=logicMgr.HeroManager.getHeroDatabyPartnerId(v.partnerId,true)
                local info={}
                info.heroId=tonumber(heroData.partnerModelId)
                info.lastLevel= v.lastLevel
                info.level=heroData.favorLv
                info.exp=heroData.exp
                info.lastExp= v.lastExp
                info.interactionId= saveData.interactionId
                if heroData.favorLv> v.lastLevel then
                    flag=true
                    local data={}
                    data.partnerId=v.partnerId
                    data.lastLevel=v.lastLevel
                    data.level=heroData.favorLv
                    table.insert(favorUpDatas,data)
                    partnerHandler.Track_InteractionLevel(info)
                end
                partnerHandler.Track_InteractionExp(info)
            end
            if flag then
                showUI(PanelNames.UIFavorUp,favorUpDatas)
            end
        end
        partnerHandler.clearInteractionSaveData()
    end
end
--好感度赠礼
function partnerHandler.ReqPartnerInteractGive(partnerId,items)
    local req=ReqPartnerInteractGiveMessage.New()
    req.partnerId=partnerId
    req.items=items
    sendReqMessage(req)
end

function partnerHandler.ResPartnerInteractGive(msg)
    if msg.state==0 then

        local heroData=logicMgr.HeroManager.getHeroDatabyPartnerId(msg.partner.partnerId,true)
        if not table.isNullOrEmpty(heroData)then
            local data={}
            data.lastLevel=heroData.favorLv
            logicMgr.HeroManager.setHeroInteraction(msg.partner)
            data.level=msg.partner.favorLv or 0
            data.partnerId=heroData.partnerId
            if data.level>data.lastLevel then
                showUI(PanelNames.UIFavorUp,{data})
            end
            FireEvent(Config.EventType.Fresh_HeroInteraction)
        end

    end
end

function partnerHandler.setInteractionSaveData(partnerDatas,interactionId)
    local data=partnerHandler.interactionSaveData
    data.isEffective=true
    data.partnerDatas=partnerDatas
    data.interactionId=interactionId
end

function partnerHandler.clearInteractionSaveData()
    partnerHandler.interactionSaveData.isEffective=false
end

function partnerHandler.isInteractionEffective()
    return partnerHandler.interactionSaveData.isEffective
end
function partnerHandler.tryReqPartnerInteract()
    local saveData=partnerHandler.interactionSaveData
    if partnerHandler.isInteractionEffective()  then
        local datas={}
        for i,v in ipairs(saveData.partnerDatas)do
            local heroData=logicMgr.HeroManager.getHeroDatabyPartnerId(v.partnerId)
            if heroData~=nil then
                table.insert(datas,v.partnerId)
            end
        end
        if not table.isNullOrEmpty(datas)then
            partnerHandler.ReqPartnerInteract(datas,saveData.interactionId)
        end
    end
end
function partnerHandler.Track_InteractionLevel(data)
    if not table.isNullOrEmpty(data)then
        TAManager.TrackByDictionary("InteractionLevel", data)
    end
end

function partnerHandler.Track_InteractionExp(data)
    if not table.isNullOrEmpty(data)then
        TAManager.TrackByDictionary("InteractionExp", data)
    end
end

------好感度------

----------英雄点赞排行
function partnerHandler.Req_HeroCommentRanks()
    local req = ReqHeroCommentRanksMessage.New()
    sendReqMessage(req)
end

function partnerHandler.Res_HeroCommentRanks(msg)
    if msg.state==0 then
        FireEvent(Config.EventType.UpdateHeroCommentRank, msg.rankList)
    end
end

function partnerHandler.Req_HeroHotRanks(type)
    local req = ReqPartnerPopularityRankMessage.New()
    req.rankType = type
    local version = logicMgr.HeroManager.GetHotHeroRankVersion(type)
    req.rankVersion = version
    sendReqMessage(req)
end

function partnerHandler.Res_HeroHotRanks(msg)
    if msg.state == 0 then
        FireEvent(Config.EventType.UpdateHeroHotRank, msg)
    end
end
--------------------------------------------------------------------------------
-- end
--------------------------------------------------------------------------------

--请求英雄分享奖励
function partnerHandler.ReqShareHeroReward(hid)

    local req = ReqShareHeroRewardMessage.New()
    req.heroModelId = hid
    sendReqMessage(req)
end

--英雄分享奖励
function partnerHandler.ResShareHeroReward(msg)
    if msg.state == 0 then
        logicMgr.UIShowManager.ShowRewardByTempItemInfo(msg.rewards)
    end
end

---------------------------------阵容推荐---开始-----------------
--请求阵容推荐数据
function partnerHandler.ReqFormationRecInfo()
    local req = ReqFormationRecInfoMessage.New();
    sendReqMessage(req)
end
--返回阵容推荐数据
function partnerHandler.ResFormationRecInfo(msg)
    if msg.state == 0 then
        logicMgr.HeroManager.setFormationRecInfo(msg.info)
        logicMgr.HeroManager.setFormationRecClicked(msg.clickList)
        FireEvent(Config.EventType.Fresh_FormationRecClick)
        FireEvent(Config.EventType.Fresh_FormationRecTask)
    end
end

--请求阵容推荐点赞
function partnerHandler.ReqFormationRecClick(id)
    local req = ReqFormationRecClickMessage.New();
    req.formationId = id;
    sendReqMessage(req)
end
--返回阵容推荐点赞
function partnerHandler.ResFormationRecClick(msg)
    if msg.state == 0 then
        logicMgr.HeroManager.updateFormationRecClickById(msg.formationId, msg.click)
        logicMgr.HeroManager.updateFormationRecClickedById(msg.formationId)
        FireEvent(Config.EventType.Fresh_FormationRecClick)
    end
end

--请求领取任务奖励
function partnerHandler.ReqFormationRecReceive(id, formationId)
    local req = ReqFormationRecReceiveMessage.New();
    req.taskId = id;
    req.formationId = formationId;
    sendReqMessage(req)
end
--返回任务奖励
function partnerHandler.ResFormationRecReceive(msg)
    if msg.state == 0 then
        showUI(PanelNames.UICommonItemGet, nil, nil, 3, msg.items);
        logicMgr.HeroManager.updateFormationRecTaskById(msg.formationId, msg.taskId)
        FireEvent(Config.EventType.Fresh_FormationRecTask)
    end
end
---------------------------------阵容推荐---结束-----------------

---------------------------------英雄置换---开始-----------------
function partnerHandler.ReqRechangePartner(partnerIdA, partnerIdB)
    local req = ReqRechangePartnerMessage.New();
    req.partnerIdA = partnerIdA;
    req.partnerIdB = partnerIdB;
    sendReqMessage(req)
end

function partnerHandler.ResRechangePartner(msg)
    if msg.state == 0 then
        if isOpenUI(PanelNames.UIHeroSwap) then
            closeUI(PanelNames.UIHeroSwap)
        end
        --原本可以走ResPartnerInfo协议同步，服务器不肯发那个消息，后续置换有新需求或者属性还是得改
        logicMgr.HeroManager.setHeroStar(msg.partnerA.partnerId, msg.partnerA.star)
        logicMgr.HeroManager.setHeroStar(msg.partnerB.partnerId, msg.partnerB.star)
        logicMgr.HeroManager.setHeroLevel(msg.partnerA.partnerId, msg.partnerA.level)
        logicMgr.HeroManager.setHeroLevel(msg.partnerB.partnerId, msg.partnerB.level)
        logicMgr.HeroManager.setHeroPower(msg.partnerA.partnerId, msg.partnerA.fightVal)
        logicMgr.HeroManager.setHeroPower(msg.partnerB.partnerId, msg.partnerB.fightVal)

        logicMgr.EquipManager.delEquip(msg.partnerA.partnerId, EnumConst.EquipPartEnum.BODY)
        logicMgr.EquipManager.delEquip(msg.partnerA.partnerId, EnumConst.EquipPartEnum.HAND)
        logicMgr.EquipManager.delEquip(msg.partnerA.partnerId, EnumConst.EquipPartEnum.HEAD)
        logicMgr.EquipManager.delEquip(msg.partnerA.partnerId, EnumConst.EquipPartEnum.LEG)
        logicMgr.EquipManager.delEquip(msg.partnerB.partnerId, EnumConst.EquipPartEnum.BODY)
        logicMgr.EquipManager.delEquip(msg.partnerB.partnerId, EnumConst.EquipPartEnum.HAND)
        logicMgr.EquipManager.delEquip(msg.partnerB.partnerId, EnumConst.EquipPartEnum.HEAD)
        logicMgr.EquipManager.delEquip(msg.partnerB.partnerId, EnumConst.EquipPartEnum.LEG)

        logicMgr.RuneManager.TaskOffHeroRune(tostring(msg.partnerA.partnerId))
        logicMgr.RuneManager.TaskOffHeroRune(tostring(msg.partnerB.partnerId))

        logicMgr.HeroLinkManager.removeLink(tostring(msg.partnerA.partnerId),0)
        logicMgr.HeroLinkManager.removeLink(tostring(msg.partnerB.partnerId),0)
        logicMgr.HeroLinkManager.updateHeroForLinkMain()

        local _weaponInfoA = logicMgr.EquipManager.tabWeapon[tostring(msg.partnerA.partnerId)]
        if _weaponInfoA then
            _weaponInfoA.level = msg.partnerA.weaponLv
            _weaponInfoA.rank = msg.partnerA.weaponRank
        end
        local _weaponInfoB = logicMgr.EquipManager.tabWeapon[tostring(msg.partnerB.partnerId)]
        if _weaponInfoA then
            _weaponInfoB.level = msg.partnerB.weaponLv
            _weaponInfoB.rank = msg.partnerB.weaponRank
        end
        showUI(PanelNames.UIHeroSwapResult, msg.partnerA, msg.partnerB)
    end
end
---------------------------------英雄置换---结束-----------------
---智慧之链继承
function partnerHandler.ReqExtendLevel(partnerIdA, partnerIdB)
    local req = ReqExtendLevelMessage.New();
    req.partnerIdA = partnerIdA;
    req.partnerIdB = partnerIdB;
    sendReqMessage(req)
end
---智慧之链继承
function partnerHandler.ResExtendLevel(msg)
    if msg.state == 0 then
        logicMgr.HeroManager.setHeroLevel(msg.partnerA.partnerId, msg.partnerA.level)
        logicMgr.HeroManager.setHeroLevel(msg.partnerB.partnerId, msg.partnerB.level)
        logicMgr.HeroLinkManager.updateHeroForLinkMain(3,{goOutLinkPartner = tostring(msg.partnerA.partnerId), goInLinkPartner = tostring(msg.partnerB.partnerId)})
        if logicMgr.HeroLinkManager.IsTopLevelOpen() then
            logicMgr.HeroManager.updateHeroBaseAttr(tostring(msg.partnerB.partnerId),false);
            partnerHandler.ReqHeroPower(0, false)
        else
            logicMgr.HeroManager.updateHeroBaseAttr(tostring(msg.partnerB.partnerId),true);
            partnerHandler.ReqHeroPower(0, true)
        end
        --local heroList = logicMgr.HeroManager.getOwnerHeroListReadOnly()
        --for _,v in pairs(heroList) do
        --    logicMgr.HeroManager.updateHeroBaseAttr(tostring(v.partnerId), false);
        --end
        --partnerHandler.ReqHeroPower(0, false)
        FireEvent(Config.EventType.Fresh_HeroLink)
        --智慧之链链接音效
        AudioManager.Play(2020)
        FireEvent(Config.EventType.ShowLinkInResult)
    end
end